<?php

/**
 * Concrete class for handling view scripts.
 *
 * @category   Zend
 * @package    Zend_View
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class WSB_Xtension_View extends Zend_View_Abstract
{

	/**
	 * @var Zend_Translate
	 */
	protected $_translate = null;

	/**
	 * @var Zend_Config
	 */
	protected $_config = null;

    /**
     * Includes the view script in a scope with only public $this variables.
     *
     * @param string The view script to execute.
     */
    protected function _run()
    {
        include func_get_arg(0);
    }

    /**
     * Eigene View-Helper
     * Aus Performancegründen sind diese direkt im View implementiert.
     */

    /**
     * @param string $string
     * @return string
     */
    public function translate($string)
	{
    	// --> Dies ist derselbe Code wie in WSB_Xtension_Base::_()
    	if(is_null($this->_translate)) {
			$this->_translate = Zend_Registry::get('Zend_Translate');
			$locale = $this->_getLocale();
			// Erstes vorhandenes Uebersetzungsfile einbinden
			foreach($this->getScriptPaths() AS $scriptPath) {
				$scriptPath = preg_replace('#views/scripts/$#', '', $scriptPath); // Neue Version
				$scriptPath = preg_replace('#views/$#', '', $scriptPath); // Alte Version
				$scriptPath .= 'etc/frontend.' . $locale.'.csv';
	        	if(is_file($scriptPath)) {
					$this->_translate->addTranslation($scriptPath, $locale);
					break;
	        	}
			}
    	}
		return $this->_translate->_($string);
		// <--
    }

    /**
     * @param float $price
     * @param integer $precision
     * @return string
     */
	public function formatPrice($price, $precision = 2)
	{
		$locale = $this->_getLocale();
		return Zend_Locale_Format::toFloat(
			$price,
			array(
				'precision' => $precision,
				'locale' => $locale
			)
		);
	}

	/**
	 * @param string $relpath
	 * @param integer $width
	 * @param integer $height
	 * @param string $alt
	 * @param array $attribs
	 * @return string
	 */
	public function image($relpath, $width=0, $height=0, $alt='', $attribs=array())
	{
		$attribs['alt'] = $alt;
		$imgObj = new WSB_Image(WSB_PATH . $relpath);
		$scaledImgObj = $imgObj->getScaledImage($width, $height);
		return $scaledImgObj->getHtml($attribs);
	}

	/**
	 * @return string
	 */
    public function headBase()
    {
		return '<base href="http://' . WSB_HOST . WSB_URL . '" />' . "\n";
    }

    /**
     * @return string
     */
    public function baseUrl()
    {
		return 'http://' . WSB_HOST . WSB_URL;
    }

	/**
	 * @param array $params
	 * @param string $anchor
	 * @param boolean $escape
	 * @return string
	 */
	public function urlThisPage($params = array(), $anchor = null, $escape = true)
	{
		$url = WSB_Xtension_Url::getInstance()->getHrefToThisPage($params, $anchor);
		return $escape ? $this->escape($url) : $url;
	}

	/**
	 * @param integer $id
	 * @param array $params
	 * @param string $anchor
	 * @param boolean $escape
	 * @return string
	 */
	public function urlToPage($id, $params=array(), $anchor='', $escape = true)
	{
		$url = WSB_Xtension_Url::getInstance()->getHrefToPage($id, $params, $anchor);
		return $escape ? $this->escape($url) : $url;
	}

	/**
	 * @param boolean $escape
	 * @return string
	 */
	public function requestUri($escape = true)
	{
		$requestUri = htmlspecialchars_decode($_SERVER['REQUEST_URI']);
		return $escape ? $this->escape($requestUri) : $requestUri;
	}

	/**
	 * @param Zend_Config $config
	 * @return void
	 */
	public function setConfig(Zend_Config $config = null)
	{
		$this->_config = $config;
	}

	/**
	 * @return mixed
	 */
	public function config($key)
	{
		return $this->_config->$key;
	}

	/**
	 * @return integer
	 */
	public function getPageId()
	{
		return WSB_Frontend::getInstance()->getPageId();
	}

	/**
	 * @param string $key
	 * @return string
	 */
	public function feuser($key)
	{
		$auth = WSB_Auth_Frontend::getInstance();
		if($auth->hasIdentity()) {
			$feuser = $auth->getIdentity();
			return $feuser->$key;
		}
		return '';
	}

	/**
	 * @param string $format
	 * @param string $timestamp
	 * @return string
	 */
	public function date($format, $timestamp=null)
	{
		if(empty($timestamp)) {
			$timestamp = time();
		}
		$date = new Zend_Date($timestamp, null, $this->_getLocale());
		return htmlentities(utf8_decode($date->get($format)));
	}

	/**
	 * @param string|array $title
	 */
	public function substitutePageTitle($title)
	{
		Zend_Registry::set('substitutePageTitle', $title);
	}

	/**
	 * @return Zend_Locale
	 */
	private function _getLocale()
	{
		if(Zend_Registry::isRegistered('Zend_Locale')) {
			$locale = Zend_Registry::get('Zend_Locale');
		} else {
			$locale = new Zend_Locale();
		}
		return $locale;
	}

}
